package com.zc.aspect;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;



/**
 * @description:
 * @author: cong
 * @create: 2024/7/27
 */
@Aspect
//@Component
@Slf4j
public class WebLogAspect {
    @Pointcut("execution(* *..*Controller.*(..))")
    public void pointcut() {}

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();

        log.info("请求地址:{},请求方式:{},请求参数:{}", request.getRequestURL(), request.getMethod(), joinPoint.getArgs());
    }

    @AfterReturning(returning = "ret", pointcut = "pointcut()")
    public void afterReturning(Object ret) {
        log.info("返回值:{}", ret);
    }
}
